package com.lul15834.orderfood.activity.user.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lul15834.orderfood.R;
import com.lul15834.orderfood.activity.manager.adapter.OrderNoDetailAdapter;
import com.lul15834.orderfood.activity.user.UserComment;
import com.lul15834.orderfood.bean.OrderBean;
import com.lul15834.orderfood.bean.OrderDetailBean;
import com.lul15834.orderfood.bean.UserComBean;
import com.lul15834.orderfood.dao.AdminDao;
import com.lul15834.orderfood.dao.OrderDao;

import java.util.List;

/**
 * 显示商家商品的adapter
 */
public class UserAdapter extends ArrayAdapter<OrderBean> {
    private List<OrderBean> listB;
    private Context context;
    public UserAdapter(@NonNull Context context, List<OrderBean> list){
        super(context, R.layout.activity_order_finish,list);
        this.context=context;
        this.listB=list;
    }
    @Override
    public View getView(int position,View convertView, ViewGroup viewGroup){
        // position数据的位置，convertView可重用的视图对象，viewGroup父容器，测量和布局子视图
        if (convertView==null){
            LayoutInflater inflater=LayoutInflater.from(getContext());
            convertView=inflater.inflate(R.layout.user_food_list,viewGroup,false);
        }
        OrderBean tem=listB.get(position);

        String userId=tem.getUserId();
        UserComBean comUser=AdminDao.getComUser(userId);//普通用户
        ImageView imageView=convertView.findViewById(R.id.list_user_order_img);
        imageView.setImageBitmap(BitmapFactory.decodeFile(comUser.getsImg()));
        TextView name=convertView.findViewById(R.id.list_user_order_name);
        name.setText(comUser.getsName());
        TextView time=convertView.findViewById(R.id.list_user_order_time);
        time.setText("下单时间: "+tem.getOrderTime());
        String address[]=tem.getOrderAddress().split("-");
        TextView people=convertView.findViewById(R.id.list_user_order_people);
        people.setText(address[0]);
        TextView Address=convertView.findViewById(R.id.list_user_order_address);
        Address.setText(address[2]);
        TextView phone=convertView.findViewById(R.id.list_user_order_phone);
        phone.setText(address[1]);
        RecyclerView list=convertView.findViewById(R.id.list_user_order_list);
        List<OrderDetailBean> detail=tem.getOrderDetailBeans();
        //在加载一个listview
        OrderNoDetailAdapter orderNoDetailAdapter=new OrderNoDetailAdapter(getContext(),detail);
        list.setLayoutManager(new LinearLayoutManager(getContext()));
        if (detail==null||detail.size()==0){
            list.setAdapter(null);
        }else {
            list.setAdapter(orderNoDetailAdapter);
            orderNoDetailAdapter.notifyDataSetChanged();
        }
        TextView sumPrice=convertView.findViewById(R.id.list_user_order_sumPrice);
        sumPrice.setText(orderNoDetailAdapter.getSumPrice());
        Button comment=convertView.findViewById(R.id.list_user_order_comment);
        TextView sta=convertView.findViewById(R.id.list_user_order_sta);
        if (tem.getOrderSta().equals("1")){
            sta.setText("订单待处理");
            comment.setVisibility(View.GONE);
        }
        if (tem.getOrderSta().equals("2")){
            sta.setText("订单已取消");
            comment.setVisibility(View.GONE);
        }
        if (tem.getOrderSta().equals("3")){
            sta.setText("订单已完成");
            comment.setVisibility(View.VISIBLE);
        }
        comment.setOnClickListener(new View.OnClickListener() {//评论订单内容
            @Override
            public void onClick(View v) {
                //进行评论处理
                Intent intent=new Intent(getContext(), UserComment.class);
                intent.putExtra("businessId",tem.getBusinessId());
                getContext().startActivity(intent);
            }
        });



        return convertView;
    }
}

